import { Database } from 'bun:sqlite';
import { drizzle } from 'drizzle-orm/bun-sqlite';
import * as schema from '../db/schema';

// 创建数据库连接
const sqlite = new Database('../../database.sqlite');
const db = drizzle(sqlite, { schema });
import { products, cases, tags } from '../db/schema';
import { nanoid } from 'nanoid';

// 插入模拟数据
export async function seedData() {
  console.log('🌱 开始插入模拟数据...');

  try {
    // 清空现有数据
    await db.delete(products);
    await db.delete(cases);
    await db.delete(tags);

    // 插入标签数据
    const tagData = [
      { id: nanoid(), name: '发光字制作', color: '#1890ff', status: 'active' as const },
      { id: nanoid(), name: 'LED显示屏', color: '#52c41a', status: 'active' as const },
      { id: nanoid(), name: '门头招牌', color: '#722ed1', status: 'active' as const },
      { id: nanoid(), name: '导视系统', color: '#fa8c16', status: 'active' as const },
      { id: nanoid(), name: '户外广告', color: '#eb2f96', status: 'active' as const }
    ];

    await db.insert(tags).values(tagData.map(tag => ({
      ...tag,
      description: `${tag.name}相关服务`,
      usageCount: 0,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString()
    })));

    console.log('✅ 标签数据插入完成');

    // 插入产品数据
    const productData = [
      {
        id: nanoid(),
        name: 'LED发光字制作',
        description: '专业制作各类LED发光字，包括不锈钢发光字、亚克力发光字、树脂发光字等',
        content: '采用优质LED灯珠和进口材料，提供不锈钢发光字、亚克力发光字、树脂发光字等多种工艺。具有亮度高、寿命长、节能环保等特点，适用于各种商业场所的标识需求。',
        type: '发光字制作',
        categoryId: 'led-letters',
        priceRange: '180-500元/字',
        images: JSON.stringify([
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=LED%20illuminated%20letters%20on%20building%20facade%2C%20bright%20commercial%20signage%2C%20night%20view&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=stainless%20steel%20LED%20letters%20manufacturing%20workshop%2C%20precision%20cutting%20and%20assembly&image_size=landscape_4_3'
        ]),
        features: JSON.stringify(['高亮度LED', '节能环保', '防水防尘', '长寿命']),
        craftTypes: JSON.stringify([
          {
            name: '不锈钢发光字',
            description: '采用304不锈钢材质，激光切割成型，内置LED灯带',
            image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=stainless%20steel%20LED%20letters%20on%20modern%20building%2C%20premium%20commercial%20signage&image_size=square_hd',
            specifications: {
              '材质': '304不锈钢',
              '厚度': '10-15cm',
              '表面处理': '拉丝/镜面',
              '防水等级': 'IP65'
            }
          },
          {
            name: '亚克力发光字',
            description: '透明亚克力材质，光线柔和均匀，适合室内外使用',
            image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=acrylic%20LED%20letters%20with%20soft%20glow%2C%20modern%20retail%20store%20signage&image_size=square_hd',
            specifications: {
              '材质': '进口亚克力',
              '厚度': '8-12cm',
              '透光率': '≥92%',
              '颜色': '多色可选'
            }
          }
        ]),
        specifications: JSON.stringify({
          'LED类型': '2835贴片LED',
          '使用寿命': '50000小时',
          '工作电压': 'DC12V/24V',
          '防护等级': 'IP65'
        }),
        sort: 1
      },
      {
        id: nanoid(),
        name: 'LED显示屏制作安装',
        description: '提供全彩LED显示屏的设计、制作、安装及维护服务',
        content: '专业提供室内外LED显示屏解决方案，包括全彩LED显示屏、小间距LED屏、透明LED屏等多种规格。画质清晰，色彩鲜艳，支持多种播放模式，适用于广告宣传、信息发布等场景。',
        type: 'LED显示屏',
        categoryId: 'led-display',
        priceRange: '3800-8000元/平米',
        images: JSON.stringify([
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=large%20outdoor%20LED%20display%20screen%20on%20building%2C%20bright%20colorful%20advertising%20display&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=indoor%20LED%20screen%20installation%20in%20shopping%20mall%2C%20high%20resolution%20display&image_size=landscape_4_3'
        ]),
        features: JSON.stringify(['高清显示', '色彩丰富', '远程控制', '节能环保']),
        craftTypes: JSON.stringify([
          {
            name: '户外全彩LED屏',
            description: '适用于户外广告投放，高亮度、防水防尘',
            image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=outdoor%20LED%20billboard%20displaying%20advertisements%2C%20high%20brightness%20commercial%20display&image_size=square_hd',
            specifications: {
              '像素间距': 'P4-P10',
              '亮度': '6000-8000cd/㎡',
              '防护等级': 'IP65',
              '工作温度': '-20°C~+60°C'
            }
          },
          {
            name: '室内小间距LED屏',
            description: '高清晰度室内显示屏，适用于会议室、展厅等场所',
            image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=high%20resolution%20indoor%20LED%20screen%20in%20conference%20room%2C%20crystal%20clear%20display&image_size=square_hd',
            specifications: {
              '像素间距': 'P1.25-P2.5',
              '亮度': '800-1200cd/㎡',
              '对比度': '5000:1',
              '色彩': '16.7M色'
            }
          }
        ]),
        specifications: JSON.stringify({
          '刷新频率': '≥1920Hz',
          '视角': '水平160°/垂直160°',
          '控制方式': '同步/异步',
          '播放格式': '视频/图片/文字'
        }),
        sort: 2
      }
    ];

    await db.insert(products).values(productData.map(product => ({
      ...product,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString()
    })));

    console.log('✅ 产品数据插入完成');

    // 插入案例数据
    const caseData = [
      {
        id: nanoid(),
        title: '万达广场导视系统制作安装项目',
        categoryId: 'shopping-mall',
        industry: '商业地产',
        description: '为万达广场提供全套导视系统设计制作服务，提升商场形象和顾客体验。',
        content: '项目包括大堂导视牌、楼层指示牌、商铺门头标识、停车场标识等全套导视系统。采用现代简约设计风格，与商场整体装修风格完美融合。使用高品质材料和LED照明系统，确保标识清晰醒目，提升顾客购物体验。',
        duration: '6个月',
        services: JSON.stringify(['设计方案', '材料选择', '制作加工', '现场安装', '售后维护']),
        images: JSON.stringify([
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=modern%20shopping%20mall%20wayfinding%20system%2C%20elegant%20directional%20signs%20and%20displays&image_size=landscape_16_9',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=shopping%20center%20lobby%20with%20illuminated%20directory%20signs%2C%20modern%20commercial%20interior&image_size=landscape_16_9',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=retail%20store%20entrance%20signs%20in%20shopping%20mall%2C%20branded%20storefront%20signage&image_size=landscape_16_9'
        ]),
        highlights: '完成200余块导视标识，提升商场形象，获得业主高度认可',
        technologies: JSON.stringify(['亚克力发光字', 'LED背光系统', '不锈钢框架', '数字印刷技术']),
        tags: JSON.stringify([tagData[0].id, tagData[2].id, tagData[3].id]),
        clientName: '万达商业管理集团',
        completedAt: '2024-01-15',
        location: '深圳市',
        projectValue: '180万元',
        challenges: '施工时间紧张，需要在营业时间外施工，标识数量多样式复杂',
        solutions: '制定详细施工计划，采用模块化设计，提前预制加工，现场快速安装。',
        featured: true,
        status: 'active' as const,
        sort: 1
      },
      {
        id: nanoid(),
        title: '华为技术有限公司企业形象标识项目',
        categoryId: 'corporate',
        industry: '科技企业',
        description: '为华为公司总部大楼制作企业LOGO标识和楼宇标识系统。',
        content: '项目涵盖企业LOGO发光字、楼层标识、部门标牌、会议室标识等全套企业形象标识系统。采用高端不锈钢发光字工艺，体现企业的科技感和品牌形象。所有标识均符合企业VI标准，确保品牌形象的统一性和专业性。',
        duration: '4个月',
        services: JSON.stringify(['VI设计配合', '工艺选择', '精密制作', '专业安装', '质保服务']),
        images: JSON.stringify([
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=corporate%20headquarters%20building%20with%20illuminated%20company%20logo%2C%20modern%20office%20architecture&image_size=landscape_16_9',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=premium%20stainless%20steel%20LED%20letters%20on%20corporate%20building%20facade%2C%20professional%20signage&image_size=landscape_16_9'
        ]),
        highlights: '成功完成企业标识制作，提升品牌形象，获得客户高度认可',
        technologies: JSON.stringify(['304不锈钢发光字', '智能控制系统', '防雷设计', '节能LED技术']),
        tags: JSON.stringify([tagData[0].id, tagData[1].id, tagData[4].id]),
        clientName: '华为技术有限公司',
        completedAt: '2023-11-20',
        location: '深圳市',
        projectValue: '320万元',
        challenges: '企业形象要求高，工艺标准严格，安装位置复杂，安全要求高',
        solutions: '采用最高工艺标准，严格质量控制，专业高空作业团队，确保安全施工。',
        featured: true,
        status: 'active' as const,
        sort: 2
      }
    ];

    await db.insert(cases).values(caseData.map(caseItem => ({
      ...caseItem,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString()
    })));

    console.log('✅ 案例数据插入完成');
    console.log('🎉 所有模拟数据插入完成！');

  } catch (error) {
    console.error('❌ 插入数据时出错:', error);
    throw error;
  }
}

// 如果直接运行此文件，则执行数据插入
if (import.meta.main) {
  seedData().catch(console.error);
}